package com.example.view.event;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.barchartview.R;

/**
 * 事件分发模块
 */
public class EventActivity extends AppCompatActivity {

    private MyViewGroup vg;
    private MyView ve;
    String TAG ="EventActivity";
    boolean isVGTouchListener =false;//是否打印viewGroup的TouchListener方法
    boolean isVETouchListener =false;//是否打印view的TouchListener方法

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        vg =  (MyViewGroup)findViewById(R.id.vg);
        ve = (MyView) findViewById(R.id.ve);
        if(isVGTouchListener) {
            vg.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //Log.d(TAG,"viewGroup onTouch:"+event.getAction());
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN://如果当前view的down事件返回true,说明这次的时候处理完了,后续的move和up事件都不会触发,也会导致onclick方法不会触发
                            Log.d(TAG, "OnTouchListener viewGroup ACTION_DOWN");
                            return false;
                        //break;
                        case MotionEvent.ACTION_MOVE:
                            Log.d(TAG, "OnTouchListener viewGroup ACTION_MOVE");
                            break;
                        case MotionEvent.ACTION_UP:
                            Log.d(TAG, "OnTouchListener viewGroup ACTION_UP");
                            break;
                    }
                    return false;
                }
            });
        }
        vg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"viewGroup OnClick:");
            }
        });

        if(isVETouchListener){
        ve.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               // MotionEvent.ACTION_DOWN  0  MotionEvent.ACTION_MOVE  1  MotionEvent.ACTION_UP  2
                switch (event.getAction()){//如果当前view的down事件返回true,说明这次的时候处理完了,后续的move和up事件都不会触发,也会导致onclick方法不会触发
                    case MotionEvent.ACTION_DOWN:
                        Log.d(TAG,"OnTouchListener MyView ACTION_DOWN");
                        return false;
                    //break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d(TAG,"OnTouchListener MyView ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d(TAG,"OnTouchListener MyView ACTION_UP");
                        break;
                }
                return false;
            }
        });
        }
        ve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"view OnClick:");
            }
        });
    }
}
